package com.zx.cloud.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.RequestLogging;
import com.zx.cloud.Shift;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.domain.LoanOrder;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.service.BatchLendingService;
import com.zx.cloud.service.LoanOrderService;
import com.zx.cloud.service.LogService;

/**
 * @ClassName: MakeLoanController
 * @Description:放款控制器   在后台的 [放款订单]-->[批量放款] 
 * @author: zhangshuai
 * @date: 2017年12月21日 下午14:15:22
 */
@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX+"/loan")
public class BatchLendingController {
	private static final Logger logger=Logger.getLogger(BatchLendingController.class);
	@Autowired
	private LoanOrderService loanOrderService;
	@Autowired
	private BatchLendingService batchLendingService;
	@Autowired
	private LogService  logService; 
	
	@RequestLogging
	@ApiOperation(value = "批量放款", notes = "借款管理-放款订单-批量放款")
	@RequestMapping(value = "/batchLending", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer>  batchLendingOrder(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody List<String> list){
		//判断前台传进来的list不为空
		try{
			if(!list.isEmpty()){
				for(int i=0;i<list.size();i++){
					if(list.get(i)==null){
						 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"订单为空");
					}
					Map<String, String> param=new HashMap<String, String>();
					param.put("orderNo", list.get(i));
					LoanOrder loanOrder=batchLendingService.getLoanOrderByOrderNo(param);
					if(loanOrder==null){
						 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"订单不存在");
					}
					
					if(userId==null){
						 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"未登录");
					}
					Long  id=loanOrder.getId();
					if(id==null){
						 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]借款订单主键值为空啦");
					}
					LoanOrder loanOrderData=loanOrderService.get(id);
					if(!"2".equals(loanOrderData.getStatus().toString())){
						 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]此借款订单不能放款");
					}
					loanOrder.setClientId(null);
					logService.addLog(userId, "借款管理-放款订单列表-放款", 0);
					int success=batchLendingService.updateLoanOrderState(loanOrder,userId);
					if(0==success){
						Shift.fatal(StatusCode.SERVER_UNKNOWN_ERROR);
					}
					logService.addLog(userId, "[借款管理-批量放款]-订单号"+loanOrder.getOrderNo(), 0);
				}
				
			}else{
				return new ObjectDataResponse<Integer>(0);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return new ObjectDataResponse<Integer>(1);
	}
}
